package com.gitee.cashzhang27.test.boot.crypt.mapper.controller;

import com.gitee.cashzhang27.test.boot.crypt.mapper.annotation.Desensitize;
import com.gitee.cashzhang27.test.boot.crypt.mapper.entity.ObjectRestResponse;
import com.gitee.cashzhang27.test.boot.crypt.mapper.entity.TableResultResponse;
import com.gitee.cashzhang27.test.boot.crypt.mapper.entity.User;
import com.gitee.cashzhang27.test.boot.crypt.mapper.mapper.UserMapper;
import java.util.List;
import lombok.AllArgsConstructor;
import org.apache.ibatis.session.RowBounds;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/07/09 15:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/users")
public class UserController {

  private UserMapper userMapper;

  @GetMapping("/insert")
  public void insert() {
    User user = User.builder()
        .nickName("大娃")
        .gender("男")
        .fullName("水智刚")
        .bankCard("6222020200054667199")
        .idCard("33010419990520105X")
        .mobilePhone("18600176548")
        .departId("159357")
        .tenantId("258654")
        .build();
    userMapper.insert(user);
  }

  @GetMapping("/insertSelective")
  public void insertSelective() {
    User user = User.builder()
        .nickName("大娃")
        .gender("男")
        .fullName("水智刚")
        .bankCard("6222020200054667199")
        .idCard("33010419990520105X")
        .mobilePhone("18600176548")
        .departId("159357")
        .tenantId("258654")
        .build();
    userMapper.insertSelective(user);
  }

  @GetMapping("/deleteByPrimaryKey/{id}")
  public void deleteByPrimaryKey(@PathVariable("id") String id) {
    userMapper.deleteByPrimaryKey(id);
  }

  @GetMapping("/deleteByExample")
  public void deleteByExample() {
    Example example = new Example(User.class);
    Criteria criteria = example.createCriteria();
    criteria.andEqualTo("idCard", "420104199408267172");
    userMapper.deleteByExample(example);
  }

  @GetMapping("/delete")
  public void delete() {
    User user = User.builder()
        .nickName("二娃")
        .gender("男")
        .fullName("宋鑫鹏")
        .idCard("420104199408267172")
        .mobilePhone("15314536754")
        .build();
    userMapper.delete(user);
  }

  @GetMapping("/updateByPrimaryKey/{id}")
  public void updateByPrimaryKey(@PathVariable("id") String id) {
    User user = User.builder()
        .id(id)
        .nickName("二娃")
        .gender("男")
        .fullName("宋鑫鹏")
        .idCard("420104199408267172")
        .mobilePhone("15314536754")
        .departId("159357")
        .tenantId("258654")
        .build();
    userMapper.updateByPrimaryKey(user);
  }

  @GetMapping("/updateByExampleSelective/{id}")
  public void updateByExampleSelective(@PathVariable("id") String id) {
    User user = User.builder()
        .id(id)
        .nickName("二娃")
        .gender("男")
        .fullName("宋鑫鹏")
        .idCard("420104199408267172")
        .mobilePhone("15314536754")
        .tenantId("258654")
        .build();
    Example example = new Example(User.class);
    example.createCriteria().andEqualTo("departId", "159357");
    userMapper.updateByExampleSelective(user, example);
  }

  @GetMapping("/updateByPrimaryKeySelective/{id}")
  public void updateByPrimaryKeySelective(@PathVariable("id") String id) {
    User user = User.builder()
        .id(id)
        .nickName("二娃")
        .gender("男")
        .fullName("宋鑫鹏")
        .idCard("420104199408267172")
        .mobilePhone("15314536754")
        .departId("159357")
        .tenantId("123456")
        .build();
    userMapper.updateByPrimaryKeySelective(user);
  }

  @GetMapping("/selectOne/{id}")
  public User selectOne(@PathVariable("id") String id) {
    User user = userMapper.selectByPrimaryKey(id);
    return userMapper.selectOne(user);
  }

  @Desensitize(clazz = User.class)
  @GetMapping("/selectByPrimaryKey/{id}")
  public User selectByPrimaryKey(@PathVariable("id") String id) {
    return userMapper.selectByPrimaryKey(id);
  }

  @Desensitize(clazz = User.class)
  @GetMapping("/selectWrapEntityByPrimaryKey/{id}")
  public ObjectRestResponse selectWrapEntityByPrimaryKey(@PathVariable("id") String id) {
    return ObjectRestResponse.ok(userMapper.selectByPrimaryKey(id));
  }

  @GetMapping("/selectByExampleAndRowBounds}")
  public List<User> selectByExampleAndRowBounds() {
    Example example = new Example(User.class);
    example.createCriteria().andEqualTo("idCard", "420104199408267172");
    return userMapper.selectByExampleAndRowBounds(example, new RowBounds(1, 1));
  }

  @Desensitize(clazz = User.class)
  @GetMapping("/select")
  public List<User> select() {
    User user = User.builder()
        .nickName("二娃")
        .gender("男")
        .fullName("宋鑫鹏")
        .idCard("420104199408267172")
        .mobilePhone("15314536754")
        .tenantId("123456")
        .build();
    return userMapper.select(user);
  }

  @Desensitize(clazz = User.class)
  @GetMapping("/selectWrapEntity")
  public TableResultResponse<User> selectWrapEntity() {
    User user = User.builder()
        .nickName("二娃")
        .gender("男")
        .fullName("宋鑫鹏")
        .idCard("420104199408267172")
        .mobilePhone("15314536754")
        .tenantId("123456")
        .build();
    List<User> userList = userMapper.select(user);
    return new TableResultResponse<>(userList.size(), userList);
  }

  @GetMapping("/selectAll")
  public List<User> selectAll() {
    return userMapper.selectAll();
  }

  @GetMapping("/selectByIds")
  public List<User> selectByIds(@RequestParam("ids") String ids) {
    return userMapper.selectByIds(ids);
  }

}